﻿// このクラスは使用していない。TODO: 注意深く削除する。
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is tyzoh.jp code.
 *
 * The Initial Developer of the Original Code is Kazutaka Kurihara.
 * Portions created by the Initial Developer are Copyright (C) 2007
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** */
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.Windows.Forms;
//using Qurihara.QBasic;

namespace Qurihara
{

	public class ScratchElement : BitmapElement
	{
		public enum ScratchStyle
		{
			Tag,
			Mosaic,
			GrayScale,
		}
		public static ScratchStyle DefaultScratchStyle = ScratchStyle.Tag;

		protected static bool isRunMode = false;
		public static bool IsRunMode
		{
			get{return isRunMode;}
			set{isRunMode = value;}
		}
		protected static bool isNotDrawn = false;
		public static bool IsNotDrawn 
		{
			get{return isNotDrawn;}
			set{
				isNotDrawn = value;
				if(isNotDrawn == true)
				{
					ScratchElementList.Clear();
				}

			}
		}

		public static FigureElementList ScratchElementList = new FigureElementList();
		public static void DrawScratch(Graphics g)
		{
			ScratchElementList.draw(g,true);
		}
		public static void DrawScratch(Bitmap bm)
		{

			Bitmap fbm;

			IEnumerator enume = ScratchElementList.GetEnumerator();
			while (enume.MoveNext())
			{
				ScratchElement fe = (ScratchElement)enume.Current;
				fbm = new Bitmap(fe.BoundingBox.Width,fe.BoundingBox.Height);
				for(int y=0;y<fe.BoundingBox.Height;y++)
				{
					for(int x=0;x<fe.BoundingBox.Width;x++)
					{
						fbm.SetPixel(x,y,
							bm.GetPixel(fe.BoundingBox.X+x,fe.BoundingBox.Y+y));
					}
				}
				switch(ScratchElement.DefaultScratchStyle)
				{
					case ScratchElement.ScratchStyle.Mosaic:

						short fine = (short)((3-fe.AnimState)*5);
                        if (fine > 0)
                        {
                            //CSharpFilters.BitmapFilter.Pixelate(fbm,fine,false);
                        }
						break;
					case ScratchElement.ScratchStyle.GrayScale:
					default:
						//CSharpFilters.BitmapFilter.GrayScale(bm);
						break;

				}
				for(int y=0;y<fe.BoundingBox.Height;y++)
				{
					for(int x=0;x<fe.BoundingBox.Width;x++)
					{
						bm.SetPixel(fe.BoundingBox.X+x,fe.BoundingBox.Y+y,
							fbm.GetPixel(x,y));
					}
				}
				if(ScratchElement.DefaultScratchStyle == ScratchElement.ScratchStyle.GrayScale) break;

			}

		}
		public static void DrawNextScratch(Graphics g)
		{
			if(ScratchElementList.Count > 0)
			{
				FigureElement element = (FigureElement)ScratchElementList[0];
				//g.FillRectangle(Brushes.Silver,element.BoundingBox);
				g.DrawRectangle(new Pen(Color.LightGreen,3),element.BoundingBox);
			}
		}

		protected static int anm_max = 3;
		protected int anm= 0;
		public int AnimState
		{
			get{return anm;}
		}
		public bool AnimStep()
		{
			anm++;
			if (anm == anm_max) 
			{
				anm = 0;
				return true;
			}
			else
			{
				return false;
			}
		}
		public ScratchElement(Control parentCont,int prio,Rectangle r) : base(parentCont,prio,r)
		{
		}
		public ScratchElement(Control parentCont,int prio,Rectangle r,Bitmap bm) : base(parentCont,prio,r,bm)
		{
		}

		public override void draw(Graphics g)
		{
			if (visible)
			{
				if (isRunMode)
				{
					if(isNotDrawn)
					{
						ScratchElementList.Add(this);
					}
					else
					{
						//g.FillRectangle(Brushes.Silver,BoundingBox);
						//g.FillRectangle(new HatchBrush(HatchStyle.LightUpwardDiagonal,Color.White,Color.LightGreen) ,BoundingBox);
						//g.DrawRectangle(Pens.LightGreen,BoundingBox);
						//g.DrawRectangle(Pens.Black,element.BoundingBox);

						float ratio = (float)(0.2 + 0.4 * anm);
						Point [] p = new Point[5];
						p[0] = BoundingBox.Location;
						p[1] = new Point(BoundingBox.Right,BoundingBox.Top);
						p[2] = new Point(BoundingBox.Right,BoundingBox.Bottom);
						p[3] = new Point(BoundingBox.Left + (int)(BoundingBox.Width * ratio),
							BoundingBox.Bottom);
						p[4] = new Point(BoundingBox.Left,
							BoundingBox.Bottom - (int)(BoundingBox.Height * ratio));
						g.FillPolygon(new HatchBrush(HatchStyle.LightUpwardDiagonal,Color.White,Color.LightGreen),p);
						g.DrawPolygon(Pens.Black,p);

						Point [] t = new Point[3];
						int a_ = (int)(BoundingBox.Height * ratio);
						int b_ = (int)(BoundingBox.Width * ratio);
						Point a = new Point(0,a_);
						Point b = new Point(b_,0);
						float ac = (float)(2 * b_ * b_) / (float)(b_ * b_ + a_ * a_);
						float bc = (float)(2 * a_ * a_) / (float)(b_ * b_ + a_ * a_);
						Point OQ = new Point((int)(ac *a.X + bc *b.X),
							(int)(ac * a.Y + bc *b.Y));
						t[0] = p[3];
						t[1] = p[4];
						t[2] = new Point(BoundingBox.Left + OQ.X,
							BoundingBox.Bottom - OQ.Y);
						g.FillPolygon(Brushes.LightYellow,t);
						//g.FillPolygon(new HatchBrush(HatchStyle.LightDownwardDiagonal,Color.White,Color.LightGreen),t);
						g.DrawPolygon(Pens.Black,t);

					}
				}
				else
				{
					g.DrawImage(image,BoundingBox);
					highlightDraw(g);
				}
			}
		}

		public override void createCopy(out FigureElement fe)
		{
			fe = (FigureElement)(new ScratchElement(this.parentControl,this.priority,this.boundingBox,this.BitMap));
		}
		public override void createCreateEvent(FigureElementList elementList,out CreateObjectEvent coe)
		{
			//coe = (CreateObjectEvent)(new CreateInkEvent(elementList,this));
			coe = (CreateObjectEvent)(new CreateBitmapEvent(elementList,this));
		}
	}
}